package com.fpost.form;

import java.text.MessageFormat;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.fpost.constants.ActionConstants;
import com.fpost.constants.CommonConstants;
import com.fpost.domain.User;
import com.fpost.manager.UserManager;
import com.fpost.util.ActionUtils;
import com.fpost.util.CommonUtils;

public class CreateAccountForm extends ActionForm {

	private String username;
	private String password;
	private String passwordConfirmation;
	private String emailAddress;
	private String emailAddressConfirmation;
	private String firstName;
	private String lastName;	

//	private String location;
//	private String dateOfBirth;

	public ActionErrors validate(ActionMapping mapping,	HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		
		if (username == null && password == null 
				&& passwordConfirmation == null && emailAddress == null) {
			return errors;
		}
		
		if ("".equals(username)) {
			errors.add("username", new ActionMessage("error.username.required"));
		} else if (username.length() > CommonConstants.USERNAME_MAX_LENGTH) {
			errors.add("username", new ActionMessage("error.username.maxlength"));
		} else {
			User user = new UserManager().getByUsername(username);
			if (user != null) {
				//setUsername(ActionConstants.EMPTY_STRING);
				//ResourceBundle resourceBundle = CommonUtils.getResourceBundle(request);
				//MessageFormat mf = new MessageFormat(resourceBundle.getString("error.username.already.taken"));
				//String errorMessage = mf.format(new Object[] {user.getUsername()});
				errors.add("username", new ActionMessage(
						"error.username.already.taken", new Object[]{"("+user.getUsername()+")"}));	
			} 			
		}
		
		if ("".equals(password)) {
			errors.add("password", new ActionMessage("error.password.required"));
		} else if (!isValidPassword(password)) {
			errors.add("password", new ActionMessage("error.password.rules"));
		}
		
		if ("".equals(passwordConfirmation)) {
			errors.add("passwordConfirmation", new ActionMessage("error.passwordConfirmation.required"));
		} else if (!password.equals(passwordConfirmation)) {
			errors.add("passwordConfirmation", new ActionMessage("error.passwordConfirmation.nomatch"));
		} 

		// if (getPassword() == null || getPassword().length() < 1) {
		// errors.add("password", new ActionMessage("error.password.required"));
		// } else if (getPassword().length() < 6) {
		// errors.add("password", new ActionMessage("error.password.minlength"));
		// }
		
		if ("".equals(emailAddress)) {
			errors.add("emailAddress", new ActionMessage("error.emailAddress.required"));
		} else if (!isValidEmailAddress(emailAddress)) {
			errors.add("emailAddress", new ActionMessage("error.emailAddress.notvalid"));
		}
		
		if ("".equals(emailAddressConfirmation)) {
			errors.add("emailAddressConfirmation", new ActionMessage("error.emailAddressConfirmation.required"));
		} else if (!emailAddress.equals(emailAddressConfirmation)) {
			errors.add("emailAddressConfirmation", new ActionMessage("error.emailAddressConfirmation.nomatch"));
		} 

		return errors;
	}

	public boolean isValidPassword(String password) {
		// at least one digit in any position
		// at least one upper or lower case character in any position
		// length 6 to 20 characters

		String expression = "^.*(?=.{6,20})(?=.*\\d)(?=.*[a-zA-Z]).*$";
		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher((CharSequence)password);
		return matcher.matches();

	}

	public boolean isValidEmailAddress(String emailAddress) {
		String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
		Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher((CharSequence)emailAddress);
		return matcher.matches();

	}

	@Deprecated
	public static void passwordValidation() {
		String password = "aaZZa44@";
		String pattern = "((?=.*[0-9])(?=.*[a-z]) (?=.*[A-Z])(?=.*[@#*=])(?=[\\S]+$).{5,10})";
		System.out.println(password.matches(pattern));

		// Explanations:
		//
		// (?=.*[0-9]) a digit must occur at least once
		// (?=.*[a-z]) a lower case letter must occur at least once
		// (?=.*[A-Z]) an upper case letter must occur at least once
		// (?=.*[@#*=]) a special character must occur at least once
		// (?=[\\S]+$) no whitespace allowed in the entire string
		// .{5,10} at least 5 to 10 characters
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getEmailAddressConfirmation() {
		return emailAddressConfirmation;
	}

	public void setEmailAddressConfirmation(String emailAddressConfirmation) {
		this.emailAddressConfirmation = emailAddressConfirmation;
	}

	public String getPasswordConfirmation() {
		return passwordConfirmation;
	}

	public void setPasswordConfirmation(String passwordConfirmation) {
		this.passwordConfirmation = passwordConfirmation;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}



}
